import json

from cli.src.db import models, schemas
from cli.src.db.database import get_db

from sqlalchemy.orm import Session


def create_order(order: schemas.OrderCreate, db: Session = next(get_db())):
    db_order = models.Order(**order.model_dump())
    db.add(db_order)
    db.commit()
    db.refresh(db_order)
    db.close()
    return db_order


def update_order(
    order_id: int, order: schemas.OrderUpdate, db: Session = next(get_db())
):
    # db_order = db.query(models.Order).filter_by(id=order_id).first()
    db_order = models.Order(**order.model_dump())
    # print(vars(db_order), vars(order))
    db.query(models.Order).filter_by(id=order_id).update(vars(order))
    db.commit()
    return db_order
